<?php

namespace app\home\controller\content\article;

use app\common\controller\Jwt;
use app\home\controller\Controller;
use app\home\model\article\Category as CategoryModel;

/**
 * 文章分类
 * Class Category
 * @package app\store\controller\content
 */
class Category extends Jwt
{
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function initialize()
    {
        parent::initialize();
        // 控制器初始化
        $this->model = new CategoryModel;
    }
    /**
     * 文章分类列表
     * @return mixed
     */
    public function list()
    {
        return $this->renderSuccess($this->model->getAll());
    }

    /**
     * 添加文章分类
     * @return array|mixed
     */
    public function add()
    {
        // 新增记录
        if ($this->model->add($this->request->param())) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError($this->model->getError() ?? '添加失败');
    }

    /**
     * 编辑文章分类
     * @param $category_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        // 更新记录
        if ($this->model->edit($this->request->param())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($this->model->getError() ?? '更新失败');
    }

    /**
     * 删除文章分类
     * @param $category_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        if (!$this->model->remove($this->request->param('category_id'))) {
            return $this->renderError($this->model->getError() ?? '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}
